﻿#include "widget.h"
#include "ui_widget.h"
#include <stdlib.h>
#include <QDebug>
#include <QMessageBox>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //数字
    connect(ui->pushButton_0, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_1, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_2, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_3, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_4, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_5, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_6, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_7, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_8, &QPushButton::clicked, this, &Widget::onNumberClicked);
    connect(ui->pushButton_9, &QPushButton::clicked, this, &Widget::onNumberClicked);
    //运算符
    connect(ui->pushButton_add, &QPushButton::clicked, this, &Widget::onOperatorCliked);
    connect(ui->pushButton_sub, &QPushButton::clicked, this, &Widget::onOperatorCliked);
    connect(ui->pushButton_mul, &QPushButton::clicked, this, &Widget::onOperatorCliked);
    connect(ui->pushButton_div, &QPushButton::clicked, this, &Widget::onOperatorCliked);
    // =
    connect(ui->pushButton_cal, &QPushButton::clicked, this, &Widget::onEqualsClicked);
    //清空
    connect(ui->pushButton_clear, &QPushButton::clicked, this, &Widget::onClearClicked);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::onNumberClicked()
{
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    QString text = button->text();
    ui->lineEdit_res->setText(ui->lineEdit_res->text() + text);
}

void Widget::onOperatorCliked()
{
    QPushButton* button = qobject_cast<QPushButton*>(sender());
    QString text = button->text();
    ui->lineEdit_res->setText(ui->lineEdit_res->text() + " " + text + " ");
}

void Widget::onEqualsClicked()
{
    QString expression = ui->lineEdit_res->text();
    QStringList list = expression.split(" ");

    if(list.size() != 3)
    {
        ui->lineEdit_res->clear();
        QMessageBox::warning(nullptr,"警告", "我只会一元运算！！！");
        return;
    }
    int a = list[0].toInt();
    int b = list[2].toInt();
    QString symbol = list[1];
    double result;
    if(symbol == "+")
    {
        result = add(a,b);
    }
    if(symbol == "-")
    {
        result = sub(a,b);
    }
    if(symbol == "*")
    {
        result = mul(a,b);
    }
    if(symbol == "/")
    {
        if(b !=0)
        {
            result = div(a,b);
        }
        else
        {
            qDebug() << "0不能作为除数";
        }
    }
    ui->lineEdit_res->setText(QString::number(result));

}

void Widget::onClearClicked()
{
    ui->lineEdit_res->clear();
}

int Widget::add(int a,int b)
{
    return a + b;
}
int Widget::sub(int a, int b)
{
    return a - b;
}
int Widget::mul(int a, int b)
{
    return a * b;
}
int Widget::div(int a, int b)
{
    return a / b;
}
